var util=require('../../utils/util.js')
const app=getApp()

Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    islogin:true,
    imgsrc:'',
    nickName:'',
    qiandao:'http://icon.tongchengyouzhi.com/qiandao_pre.png',
    maskHeight:'',
    isSign:true,
    money:'',
    integral:'',
    type:'',
    teacherShow:true,
    companyShow:true,
    vipSrc: 'http://icon.tongchengyouzhi.com/gereng/VIP_1.png'
  },
  toXiugai:function(event){
    wx.navigateTo({
      url: '../xiugai/xiugai',
    })
  },
  becomeVIP:function(event){
    wx.navigateTo({
      url: '../becomeVIP/becomeVIP',
    })
  },
  toCourse:function(event){
    wx.navigateTo({
      url: '../course/course',
    })
  },
  toInvite:function(event){
    wx.navigateTo({
      url: '../invite/invite',
    })
  },
  teacherIn:function(event){
    wx.navigateTo({
      url: '../teacherIn/teacherIn',
    })
  },
  toMydata:function(){
     wx.navigateTo({
       url: '../myData/mydata',
     })
  },
  sign:function(e){

    var that=this
    // wx.request({
    //   url: 'user/sign/recent',
    //   header: {
    //     'cookie': "JSESISONID=" + wx.getStorageSync('sessionID')
    //   },
    //   method: 'GET',
    //   success: function (res) {
    //     console.log(res)
    //   },
    //   fail: function (res) {
    //     wx.showToast({
    //       title: '查询签到信息失败',
    //       icon: 'none'
    //     })
    //   }
    // })
    // if(!this.data.isSign){
    //   wx.showToast({
    //     title: '签到成功',
    //   })
    //   this.setData({
    //     isSign:!this.data.isSign,
    //     qiandao: 'http://icon.tongchengyouzhi.com/image//%E7%AD%BE%E5%88%B0%20%E5%90%8E.png'
    //   }),
    //     app.qingqiu('user/sign', '', 'POST', this.signCallBack)
    // }else if(this.data.isSign){
    //     wx.showToast({
    //       icon:'none',
    //       title: '您今天已经签过到了哦',
        
    //     })
    // }
     wx.request({
       url: app.globalData.baseUrl+'user/sign',
       header:{
         'cookie':"JSESSIONID="+wx.getStorageSync('sessionID'),
         'content-type': 'application/x-www-form-urlencoded'
       },
       method:'POST',
       success:function(res){
        //  console.log('显示签到结果')
         console.log(res)
         if(res.data.msg=='请求成功'){
           wx.showToast({
             title: res.data.msg,
           })
           that.setData({
             qiandao:'http://icon.tongchengyouzhi.com/qiandao_after.png'
           }) 
           that.onShow()
         }else{
           wx.showToast({
             title: res.data.msg,
             icon:'none'
           }) 
         }
      
       },
       fail: function (res) {
         //  console.log('显示签到结果')
         //  console.log(res)
         wx.showToast({
           title: '签到失败'+res.data.msg,
         })
       },
     })


  },
  congzhi:function(event){
    console.log(event)
    var integral=event.detail.integral
     wx.navigateTo({
       url: '../congzhi/conzhi?integral='+integral,
     })
  },
  signCallBack:function(res){
     console.log(res)
  },
  companyIn:function(event){
    wx.navigateTo({
      url: '../companyIn/companyIn',
    })
  },
  check: function () {
    
  },
  /**
   * 生命周期函数--监听页面加载
   */ 
  
  sessionBack:function(res){
   console.log(res)
  },
  onLoad: function (options) {
   
  },
  alterLogin:function(event){
    wx.showModal({
      title: '',
      content: '请点击登录按钮',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that=this
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          maskHeight:res.windowHeight-164
        })
      },
    })
  },
  getuserinfo:function(e){

    console.log('getuserInfo')
    var that = this
     var code,nickName,gender,language,city,country,avatarUrl,position,latitude,longitude
    wx.getUserInfo({
      success:function(res)
      {
        console.log(res)
         nickName=res.userInfo.nickName
         gender=res.userInfo.gender
         language=res.userInfo.language
         city=res.userInfo.city
         country=res.userInfo.country
         avatarUrl=res.userInfo.avatarUrl
         that.setData({
           imgsrc:avatarUrl,
           nickName:nickName
         })
         //获取位置
        wx.getLocation({
          success: function (res) {

            console.log('location')
            console.log(res)
            latitude = res.latitude
            longitude = res.longitude
        

        //登录
            wx.login({
              success(res) {
                // console.log("res:")
                 console.log(res)
                code = res.code

              //  console.log(code)
              //  console.log(nickName)
              wx.request({
                url: app.globalData.baseUrl+'user/login',
                header:{
                  'content-type': 'application/x-www-form-urlencoded'
                },
                data:{
                  code: code,
                  nickName: nickName,
                  gender: gender,
                  language: language,
                  city: city,
                  country: country,
                  avatarUrl: avatarUrl,
                  position: '1111',
                  latitude: latitude,
                  longitude: longitude
                },
                method:'POST',
                success:function(res){
                  console.log('登录成功')
                  console.log(res)
                  try{
                    that.setData({
                      money:res.data.data.user.money,
                      integral:res.data.data.user.integral
                    })
                     wx.setStorage({
                      key: 'integral',
                      data: that.data.integral,
                    })
                  }catch(err){
                    console.log(err)
                    wx.showToast({
                      title: '获取钱包数据失败',
                      icon:'none'
                    })
                  }
                  try{
                    var today = util.formatTime(new Date()).substring(8, 10)
                    console.log(today)
                    var signDate = res.data.data.user.userSignList[0].gmtCreateFormat.substring(8, 10)
                    console.log(signDate)
                    if (today == signDate) {
                      that.setData({
                        qiandao: 'http://icon.tongchengyouzhi.com/qiandao_after.png'
                      })
                    }
                  }catch(err){

                  }
                  
                  wx.setStorage({
                    key: 'sessionID',
                    data: res.data.data.sessionId,
                  })
                  wx.setStorage({
                    key: 'userID',
                    data: res.data.data.user.id,
                  })
                },
                fail:function(res){
                  // wx.showToast({
                  //   title: '登录失败',
                  // })
                  console.log('失败')
                  console.log(res)
                }
              })
               
                that.setData({
                  islogin:!that.data.islogin
                })
                console.log('成功')
              //  wx.request({
              //    url: 'http://chensanwa.top/eto/user/login',
              //    method:"POST",
                
              //    data:{
              //     nickName: nickName,
              //     gender: gender,
              //     language: language,
              //     city: city,
              //     country: country,
              //     avatarUrl: avatarUrl,
              //     position: '',
              //     latitude: latitude,
              //     longtitude: longtitude
              //   },
              //    header: {
              //      "content-type":"application/x-www-form-urlencoded"
              //    },
              //   success:function(res){
              //     console.log('res')
              //     console.log(res)
              //   }
              //  })
              }
            })
          },
          fail:function(err){
            console.log(err)
            wx.showModal({
              title: '获取地理位置失败',
              content: '请打开gps定位后再登录小程序',
              showCancel:false,
              confirmText:'打开'
            })
          }
         })
        

       

      },
     
    })
    
 
   
  },
  login:function(event){
    wx.login({
      success:function(res){
        console.log(res)
      }
    })
  },
  toMoney:function(event){
    wx.navigateTo({
      url: '../qianbao/qianbao?money='+this.data.money,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    wx.request({
      url: app.globalData.baseUrl + 'user/id',
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      method: 'GET',
      success: function (res) {
        console.log(res)
        that.setData({
          type: res.data.data.type

        })

      }
    })
    console.log('type')
    console.log(that.data.type)
    if (that.data.type == 2) {
      that.setData({
        companyShow: false
      })

    } else if (that.data.type == 4) {
      that.setData({
        teacherShow: false
      })
    }

    
    wx.request({
      url: app.globalData.baseUrl + 'user/id',
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      success: function (res) {
        // console.log(res)
        // console.log(res.data.data.vip)
        try{
          if (res.data.data.vip != null) {
            that.setData({
              vipSrc: 'http://icon.tongchengyouzhi.com/gereng/VIP_2.png'
            })
            wx.setStorage({
              key: 'vip',
              data: true,
            })
          }else{
            wx.setStorage({
              key: 'vip',
              data: false,
            })
          }
        }catch(err){

        }
        
        //  var today = util.formatTime(new Date())
        try {

          var today = util.formatTime(new Date()).substring(8, 10)
          console.log(today)
          var signDate = res.data.data.userSignList[0].gmtCreateFormat.substring(8, 10)
          console.log(signDate)
          if (today == signDate) {
            that.setData({
              qiandao: 'http://icon.tongchengyouzhi.com/qiandao_after.png'
            })
          } else {
            that.setData({
              qiandao: 'http://icon.tongchengyouzhi.com/qiandao_pre.png'
            })
          }


        } catch (err) {

        }

        if (res.data.msg == '请求成功') {
          try {
            that.setData({
              money: res.data.data.money,
              integral: res.data.data.integral
            })
            console.log(that.data.integral)
            wx.setStorage({
              key: 'integral',
              data: that.data.integral,
            })
          } catch (err) {
            console.log(err)
            wx.showToast({
              title: '获取钱包数据失败',
              icon: 'none'
            })
          }
          console.log(that.data.money)
          that.setData({
            islogin: false,
            imgsrc: res.data.data.avatarUrl,
            nickName: res.data.data.nickName
          })
        } else {
          wx.login({

          })
          that.setData({
            islogin: true
          })
        }
      }
    })



    console.log(app)
    var that = this
    // wx.checkSession({
    //   success:function(){
    //     console.log('session未过期')
    //     that.setData({
    //       islogin:!that.data.islogin
    //     })
    //     console.log(that.data.islogin)
    //     app.qingqiu('user/id','','',that.sessionBack)
    //   },
    //   fail:function(){
    //     wx.showToast({
    //       title: '请点击登录按钮来登录',
    //     })
    //   }
    // })
    //console.log('start')
    // console.log('response')
    //  app.qingqiu('user/get/id', { userId: 1000 },'',this.callbackFun)
    //  app.callback(res)


    // that.setData({
    //   qiandao: 'http://icon.tongchengyouzhi.com/qiandao_after.png'
    // })
      // var url='user/get/id'
      // var data={
      //   userId=1000
      // }
      // wx.checkSession({
      //    success:function(res){
      //       that.setData({

      //         islogin:true
      //       })
      //    },
      //    fail:function(res){
      //      that.setData({
      //        islogin:false
      //      })
      //    }
      // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
   
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      this.onShow()
      wx.showToast({
        icon:'none',
        title: '刷新成功'
      })
      wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  callbackFun: function (res) {
    console.log('callback')
    console.log(res)
  }
})